package shop.maytinh.controller;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.transform.Transformers;

import shop.maytinh.util.HibernateUtil;
import shop.maytinh.model.*;


 

public class CatalogiesManager extends HibernateUtil {
	
	static List<Category> listcategory;
	public Category category = null; 
	
	public static List<Category> getlistCategory() {
		
				
	    try{
	    	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			listcategory = null;
		    session.beginTransaction();	
	    	listcategory = (List<Category>)session.createQuery("from Category").list();	    	
			if(listcategory==null)
				return null;
			session.getTransaction().commit();
			
		}
		catch(Exception e){
			 e.printStackTrace();			
		}
		return listcategory;
	    
    }	
   
public static List<Product> getProductByIdCatalogy(Integer id) {			   
        try {
        	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        	
        	List<Product> listproduct = null;
        	session.beginTransaction();
        	
            String hql = "FROM Product E WHERE E.categogy.id = :ex";
            Query query = session.createQuery(hql);
            query.setParameter("ex",id);
            listproduct = (List<Product>)query.list();        
            return listproduct;
        } catch (Exception e) {           
           return null;
        }		
    }

}



